package com.stooscloud.provider.config;

import config.properties.WsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Order
@Slf4j
public class TransInitRunner implements CommandLineRunner {
    @Resource
    WsProperties wsProperties;

    /**
     * 交易码对应的doman类
     */
    public Map<String,String> builderMap = new HashMap<>();
    /**
     * 发起系统以及交易码(是否业务处理)
     */
    public Map<String,Boolean> busMap = new HashMap<>();
    @Override
    public void run(String... args) throws Exception {
        log.info("###初始化交易数据开始.........");
        if (wsProperties.getBuilderList() !=null && !wsProperties.getBuilderList().isEmpty()){
            for (String val : wsProperties.getBuilderList()){
                String key = StringUtils.substringBefore(val,":");
                String value = StringUtils.substringAfter(val,":");
                builderMap.put(key,value);
            }
        }
        if (wsProperties.getBusList() !=null && !wsProperties.getBusList().isEmpty()){
            for (String val : wsProperties.getBusList()){
                String key = StringUtils.substringBefore(val,":");
                Boolean value = Boolean.parseBoolean(StringUtils.substringAfter(val,":"));
                busMap.put(key,value);
            }
        }
        log.info("###初始化交易数据结束.........");
    }

    /**
     * 获取构建器
     * @param transCode
     * @return
     */
    public String getBuilder(String transCode){
        return builderMap.get(transCode);
    }

    /**
     * 交易是否业务处理
     * @param systemId_transCode
     * @return
     */
    public boolean idBus(String systemId_transCode){
        return busMap.get(systemId_transCode);
    }
}
